package com.codes.config.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MywebConfig implements WebMvcConfigurer {



    @Value("${file-save-path}")
    private String fileSavePath;
    @Value("${file-save-local-path}")
    private String fileSaveLocalPath;


    @Autowired
    private MyInterceptor myInterceptor;

//    @Override  //请求的路径直接 指向视图层
//    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/zxc/foo").setViewName("foo");
//    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                // addPathPatterns 用来设置拦截路径，excludePathPatterns 用来设置白名单
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login","/login.html","/page/404.html",
                        "/login","/json",".js",".css",".ico",".jpg",".png",
                        "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/images/*",
                        "/**/*.ico",
                        "/information/page",// 忽略轮播图列表接口
                        "**/doc.html",
                        "/css/**","../images/**","/js/**",
                        "/layui/**",".js")
                ;

    }

    /**
     * 添加静态资源--过滤swagger-api (开源的在线API文档)
     * 通过addResourceLocations来指定路径。我们访问自定义my文件夹中的elephant.jpg
     * 图片的地址为 http://localhost:8080/my/elephant.jpg
     * @param registry
     */
    @Override  //自定义资源映射
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        /**
         * 配置资源映射
         * 意思是：如果访问的资源路径是以“/images/”开头的， fileSaveLocalPath
         * 就给我映射到本机的“E:/images/”这个文件夹内，去找你要的资源 fileSavePath
         * 注意：E:/images/ 后面的 “/”一定要带上
         */

        registry.addResourceHandler(fileSaveLocalPath+"**")
                .addResourceLocations("file:"+fileSavePath);

    }

//    /**
//     * 跨域支持
//     * @param registry
//     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowCredentials(true)
//                .allowedMethods("GET", "POST", "DELETE", "PUT")
//                .maxAge(3600 * 24);
//    }

}